
package GUI;

/**
 *
 * @author David Ponce
 * @author Philipp Armbruster
 */

import Interaction.ServerConnection;
import Flightadministration.Flightadministration;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;



public class AirlineCreate extends JPanel{

    final Main td;
    private JPanel jp;
    /* Das AdministrationsObjekt, welches über die getConnection methode vom RMI Server geholt wird, dem "admin" objekt zugewiesen.
     * Dadurch ist eine Kommunikation zwischen GUI und Applikationsserver mittels RMI möglich*/
    final Flightadministration admin = ServerConnection.getConnection();


    // Konstruktor
    public AirlineCreate(final Main td) {
        this.td = td;
        showAirlineCreate();
    }


    // Methoden
    public void showAirlineCreate() {

        jp = new JPanel();

        //GridBagLayout
        //**********************************************************************

        GridBagLayout gbl = new GridBagLayout();
        jp.setLayout(gbl);
        jp.setBackground(Color.white);

        GridBagConstraints constraints = new GridBagConstraints();

        constraints.fill = GridBagConstraints.HORIZONTAL;   // wie Komponente Bereich füllen soll
        constraints.weightx = 1;                     // Breite
        constraints.weighty = 3;                    // Höhe
        constraints.insets = new Insets(0,0,1,1);   // Abstände definieren


        //Label
        //**********************************************************************

        JLabel headline = new JLabel("<html><h2 color=blue>Fluglinie anlegen</h2></html>");
        headline.setHorizontalAlignment(JLabel.CENTER);
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 2;
        jp.add(headline, constraints);

        JLabel label1 = new JLabel("Name: ");
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.gridwidth = 1;
        jp.add(label1, constraints);

        final JTextField textfield1 = new JTextField();
        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.gridwidth = 1;
        jp.add(textfield1, constraints);


        //Buttons
        //**********************************************************************

        JButton button_CreateAirline = new JButton("Fluglinie anlegen");
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        jp.add(button_CreateAirline, constraints);

        JButton button_Cancel = new JButton("Zurück zur Übersicht");
        constraints.gridx = 1;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        jp.add(button_Cancel, constraints);

        
        //Action Listener
        //**********************************************************************

        button_CreateAirline.addActionListener(new ActionListener() {
                @Override
        public void actionPerformed(ActionEvent e){
            if ((textfield1.getText().equals("")))
            {
                JOptionPane.showMessageDialog(null, "Bitte Name angeben!");
            }
            else {
                        try {
                            admin.createAirline(textfield1.getText());
                            td.updateAirline();

                            JOptionPane.showMessageDialog(null, "Fluglinie angelegt");
                            textfield1.setText("");

                            ((CardLayout) td.rightp.getLayout()).show(td.rightp, "AirlineM");

                        } catch (RemoteException ex) {
                            Logger.getLogger(AircraftCreate.class.getName()).log(Level.SEVERE, null, ex);
                        }
             }
            }
        });

        button_Cancel.addActionListener(new ActionListener() {
                @Override
        public void actionPerformed(ActionEvent e){
                    ((CardLayout) td.rightp.getLayout()).show(td.rightp,"AirlineM");

            }
        });
    }


    public JPanel getJp() {
        return jp;
    }
}